﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StopWatch
{
    public partial class SettingsForm : Form
    {
        StopWatchSettings _settings;
        public SettingsForm(StopWatchSettings settings)
        {
            InitializeComponent();
            _settings = settings;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (radioButtonUseFigure.Checked)
            {
                _settings.EndTime = DateTime.Now + new TimeSpan((int)numericUpDownHours.Value, (int)numericUpDownMinutes.Value, (int)numericUpDownSeconds.Value);
            }
            else
            {
                _settings.EndTime = endDateTimePicker.Value;
            }

            _settings.UseFigures = radioButtonUseFigure.Checked;
            _settings.ShowElapsedTime = radioButtonElapsedTime.Checked;
            CloseForm(DialogResult.OK);
        }

        private void CloseForm(DialogResult dialogResult)
        {
            DialogResult = dialogResult;
            Close();

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            CloseForm(DialogResult.Cancel);
        }

        private void radioButtonUseFigure_CheckedChanged(object sender, EventArgs e)
        {
            EnableControls(radioButtonUseFigure.Checked);
        }

        private void EnableControls(bool enable)
        {
            numericUpDownHours.Enabled = enable;
            numericUpDownMinutes.Enabled = enable;
            numericUpDownSeconds.Enabled = enable;
            endDateTimePicker.Enabled = !enable;
        }

        private void radioButtonUseDateTimePicker_CheckedChanged(object sender, EventArgs e)
        {
            EnableControls(!radioButtonUseDateTimePicker.Checked);
        }

        private void endDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            if (DateTime.Now > endDateTimePicker.Value)
            {
                MessageBox.Show("End Time Should be greater than current Time");
                endDateTimePicker.Value = DateTime.Now;
            }
            else
            {
                _settings.EndTime = endDateTimePicker.Value;
                SetCalculatedTime();
            }
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            radioButtonUseFigure.Checked = _settings.UseFigures;
            endDateTimePicker.Value = _settings.EndTime;
            radioButtonUseDateTimePicker.Checked = !_settings.UseFigures;
            if (_settings.ShowElapsedTime)
            {
                radioButtonElapsedTime.Checked = true;
            }
            else
            {
                radioButtonRemainingTime.Checked = true;
            }
            SetCalculatedTime();
        }

        private void SetCalculatedTime()
        {
            if (_settings.EndTime > DateTime.Now)
            {
                TimeSpan remainingTime = _settings.EndTime - DateTime.Now;
                numericUpDownSeconds.Value = remainingTime.Seconds;
                numericUpDownMinutes.Value = remainingTime.Minutes;
                numericUpDownHours.Value = remainingTime.Hours;
            }
            else
            {
                numericUpDownSeconds.Value = 0;
                numericUpDownMinutes.Value = 0;
                numericUpDownHours.Value = 0;
            }
        }

        private void numericUpDownHours_ValueChanged(object sender, EventArgs e)
        {
            UpdateEndDateTimePicker();
        }

        private void UpdateEndDateTimePicker()
        {
            endDateTimePicker.Value = DateTime.Now.Add(new TimeSpan((int)numericUpDownHours.Value, (int)numericUpDownMinutes.Value, (int)numericUpDownSeconds.Value));
        }

        private void numericUpDownMinutes_ValueChanged(object sender, EventArgs e)
        {
            UpdateEndDateTimePicker();
        }

        private void numericUpDownSeconds_ValueChanged(object sender, EventArgs e)
        {
            UpdateEndDateTimePicker();
        }        
    }
}
